#ifndef TCLIENTSERVER_H
#define TCLIENTSERVER_H
#include "tserialize.h"
#include <QObject>
#include <QTcpSocket>
#include <QTcpServer>

enum class OPERATOR
{
   
    RECIVE_MESSAGE,
	SEND_MESSAGE
};

class TClientServer : public QObject
{
    Q_OBJECT
public:
	QTcpSocket* m_pSocket;
    static TClientServer* Instance();
    void takeAction(QByteArray byteArray);
	
	~TClientServer();
	template<typename Pointer, typename Func>
	inline void sendMessage(
		const QString& sender_,
		const QString& receiver,
		const QString& messageType,
		const QString& content,
		const QString& time,
		Pointer* caller,
		Func callBack)
	{

		m_pSocket->flush();
		m_pSocket->write(TSerialize::serialize(
			OPERATOR::SEND_MESSAGE,
			sender_,
			receiver,
			messageType,
			content,
			time
		));//将数据写给服务器
		connect(this, &TClientServer::verifyAccountReply, caller, callBack, Qt::UniqueConnection);//链接一次
	}

private:signals:
    void receiveMessageNotice(QByteArray);
	void verifyAccountReply(QByteArray);
private:
    TClientServer(const TClientServer&) = delete;
    TClientServer(TClientServer&&) = delete;
    TClientServer(QObject* parent = nullptr);
	void onSocketReadyRead();
    void parseByteArrayPackets(QByteArray byteArray);

	void receiveMessage(const QByteArray& byteArray);

};

#endif // TCLIENTSERVER_H
